using System;
using System.Collections.Generic;
using System.Threading;

namespace Admin.NET.Application.GuangHeMeter;

public class AsyncUserTokenPool
{
	private Queue<AsyncUserToken> m_pool;

	public int Count => m_pool.Count;

	public AsyncUserTokenPool(int capacity)
	{
		m_pool = new Queue<AsyncUserToken>(capacity);
	}

	public void Push(AsyncUserToken item, Semaphore _maxAcceptedClients, ref int count)
	{
		if (item == null)
		{
			throw new ArgumentNullException("");
		}
		lock (m_pool)
		{
			if (!m_pool.Contains(item))
			{
				m_pool.Enqueue(item);
				count = _maxAcceptedClients.Release();
			}
		}
	}

	public void Push(AsyncUserToken item)
	{
		if (item == null)
		{
			throw new ArgumentNullException("");
		}
		lock (m_pool)
		{
			if (!m_pool.Contains(item))
			{
				m_pool.Enqueue(item);
			}
		}
	}

	public AsyncUserToken Pop()
	{
		lock (m_pool)
		{
			AsyncUserToken popToken = m_pool.Dequeue();
			popToken.InitDisPose();
			return popToken;
		}
	}
}
